<template>
    <div class="addCommodity-box">
        <div class="addCommodity-one">
            <div class="addCommodity-left">添加课程</div>
            <div class="addCommodity-right">
                <el-button class="edit_btn" size="small" @click="$router.push('/class/list')"><i class="el-icon-arrow-left"></i><span>返回</span></el-button>
            </div>
        </div>
        <div class="addCommodity-two">
            <div class="addCommodity-two-xinxi">
                <div class="right"></div>
                <div class="addCommodity-two-ding"><i class="el-icon-collection-tag" style="color: #fff;font-size: 18px;"><div>添加课程信息</div></i></div>
            </div>
            <div class="addCommodity-two-form">
                <el-form label-width="150px"
                         ref="jsCourse"
                         :model="jsCourse"
                         style="text-align: left"
                         :rules="rule">
                    <el-form-item label="课程分类：" prop="classId">
                        <el-input v-model="jsCourse.classId" placeholder="请选择课程分类" >

                        </el-input>
                        <a id="popup" @click="xzfl"><img src="/images/icon1.gif"></a>
                    </el-form-item>
                    <el-form-item label="课程名称：" prop="courseName">
                        <el-input v-model="jsCourse.courseName" placeholder="请输入课程名称"></el-input>
                    </el-form-item>
                    <el-form-item label="课程介绍：" prop="courseIntro">
                        <el-input type="textarea" :rows="6" style="width: 300px" v-model="jsCourse.courseIntro" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="课程售价：" prop="courseNprice">
                        <el-input v-model="jsCourse.courseNprice" placeholder="请输入价格"></el-input>
                    </el-form-item>
                    <el-form-item label="课程上架：" prop="courseStatus">
                        <el-switch
                                v-model="jsCourse.courseStatus"
                                active-color="#13ce66"
                                inactive-color="#CCC"
                                active-value="2"
                                inactive-value="3">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="课程关键字：" prop="coursePrimary">
                        <el-input v-model="jsCourse.coursePrimary" placeholder="请输入课程关键字"></el-input>
                    </el-form-item>
                    <el-form-item label="上传视频封面图：" prop="url">
                        <el-upload
                                class="avatar-uploader"
                                action="/upload/addimg"
                                :show-file-list="false"
                                :auto-upload="false"
                                list-type="picture-card"
                                :before-upload="beforeUpload"
                                :on-success="handleAvatarSuccess"
                                >
                            <i slot="default" class="el-icon-plus"></i>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img :src="dialogImageUrl" alt="">
                        </el-dialog>
                    </el-form-item>
                    <el-form-item label="上传视频：" prop="">
                        <el-upload
                            class="avatar-uploader el-upload--text"
                            action="/upload/addvideo"
                            :show-file-list="false"
                            :on-success="handleVideoSuccess"
                            :before-upload="beforeUploadVideo"
                            :on-progress="uploadVideoProcess"
                        >
                        </el-upload>
                        <el-button icon="el-icon-upload"> 上传视频 </el-button>
                    </el-form-item>
                    <el-form-item label="预览：" v-if=courseUrlyl >
                        <video
                            v-if="jsCourse.courseUrl != '' && videoFlag == false"
                            :src="jsCourse.courseUrl"
                            class="avatar"
                            controls="controls"
                        >
                            您的浏览器不支持视频播放
                        </video>
                        <i
                            v-else-if="jsCourse.courseUrl == '' && videoFlag == false"
                            class="el-icon-plus avatar-uploader-icon"></i>
                        <el-progress
                            v-if="videoFlag == true"
                            type="circle"
                            :percentage="videoUploadPercent"
                            style="margin-top:30px;"
                        ></el-progress>
                    </el-form-item>
                    <el-form-item label="备注：" prop="courseInfo">
                        <el-input type="textarea" :rows="5" style="width: 300px" v-model="jsCourse.courseInfo" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="addCourse(jsCourse)"> 提交课程 </el-button>
                    </el-form-item>

                    <!--<div id="videoPreview" class="layui-form-item layui-none">
                        <label class="layui-form-label">视频预览：</label>
                        <div class="layui-input-block">

                            <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                                <div class="layui-progress-bar" lay-percent=""></div>
                            </div>
                            <input type="hidden" name="courseUrl" required lay-verify="required"/>
                            &lt;!&ndash;课程时长单位分钟&ndash;&gt;
                            <input type="hidden" name="courseTime">
                        </div>
                    </div>
                    &lt;!&ndash;用户id&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.userId" hidden></el-input>&ndash;&gt;
                    &lt;!&ndash;创建人id&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.createId" hidden ></el-input>&ndash;&gt;
                    &lt;!&ndash;2新品&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.recommend" hidden value="2"></el-input>&ndash;&gt;
                    &lt;!&ndash;上架状态3未上架&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.courseStatus" hidden value="3"></el-input>&ndash;&gt;
                    &lt;!&ndash;审核状态3未审核&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.courseCstatus" hidden value="3"></el-input>&ndash;&gt;
                    &lt;!&ndash;课程序号&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.courseNo" hidden value="1"></el-input>&ndash;&gt;
                    &lt;!&ndash;点赞数量&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.likeNum" hidden value="0"></el-input>&ndash;&gt;
                    &lt;!&ndash;分享数量&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.shareNum" hidden value="0"></el-input>&ndash;&gt;
                    &lt;!&ndash;收藏数量&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.collectNum" hidden value="0"></el-input>&ndash;&gt;
                    &lt;!&ndash;预览数量&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.lookNum" hidden value="0"></el-input>&ndash;&gt;
                    &lt;!&ndash;销售数量&ndash;&gt;
        &lt;!&ndash;            <el-input v-model="jsCourse.saleNum" hidden value="0"></el-input>&ndash;&gt;-->

                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "AddCourse",
        data(){
            return{
                jsCourse:{
                    classId:"",         //课程专业id
                    courseName:"",      //课程名称
                    courseNprice:"",    //课程现价
                    courseIntro:"",     //课程简介
                    courseInfo:"",      //课程详情
                    createId:"",        //创建人id
                    courseStatus:"",    //上架状态（2、上架  3、未上架）
                    coursePrimary:"",   //课程搜索关键词
                    url:"",             //图片封面url地址
                    courseUrl:"",       //课程视频
                },
                dialogVisible:false,    //图片预览展示
                dialogImageUrl:'',      //图片封面
                courseUrlyl:false,      //视频预览
                videoFlag:true,        //视频播放
                //添加的时候必填项验证
                rule: {
                    classId:[
                        {required:true,message:'课程分类不可以为空'},
                        {message:'请选择分类'}
                    ],
                    courseName:[
                        {required:true,message:'课程名称不可以为空'},
                        {max:10,message:'最大长度为 10'}
                    ],
                    courseIntro:[
                        {required:true,message:'课程介绍不可以为空'},
                        {message:'请输入内容'}
                    ],
                    courseNprice:[
                        {required:true,message:'课程售价不能为空'},
                        {message:'售价必须为数字值'}
                    ],
                },
            }
        },
        methods:{
            //上传视频 验证视频格式
            beforeUploadVideo(file) {
                if (
                    [
                        'video/mp4',
                        'video/ogg',
                        'video/flv',
                        'video/avi',
                        'video/wmv',
                        'video/rmvb'
                    ].indexOf(file.type) === -1
                ) {
                    this.$message.error('请上传正确的视频格式')
                    return false
                }
            },
            // 上传进度显示
            uploadVideoProcess(event, file, fileList) {
                console.log(event.percent, file, fileList)
                this.videoFlag = true
                this.videoUploadPercent = Math.floor(event.percent)
            },

            // 获取上传视频地址
            handleVideoSuccess(res, file) {
                this.videoFlag = false
                this.videoUploadPercent = 0
                if (res.status === 200) {
                    console.log(res.data)
                    this.jsCourse.courseUrl = res.data
                } else {
                    this.$message.error('视频上传失败，请重新上传！')
                }
            },

            //图片上传
            beforeUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error("上传图片大小不能超过 2MB!");
                }
                return isLt2M;
            },
            handleAvatarSuccess(file) {
                console.log(file);
                this.jsCourse.url = file.name;
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            //提交
            addCourse(jsCourse){
                console.log(jsCourse);
                //validate ：需要表单重新验证一下
                this.$refs.jsCourse.validate((valid) =>{
                    if (valid){
                        this.$http.post("/jsCourse/addjsCourse",this.jsCourse)
                            .then(res =>{
                                alert("添加成功");
                                $router.push('/class/list');
                            }).catch(err =>{
                                alert("添加失败");
                                console.log(this.jsCourse);
                        })
                    } else{
                        return false;
                    }
                })

            },

            //选课程的分类
            xzfl(){
                console.log("弹窗进行 选择课程分类");
            },
        }
    }
</script>

<style scoped>
    #popup {
        position: relative;
        left: 10px;
        top: 2px;
    }
    #imgs li{
        list-style: none;
        float: left;
        margin-right: 10px;
    }
    #imgs li img{
        width: 120px;
        height: 120px;
    }
    .el-input{
        width: 250px;
    }
    /*在外边套一个盒子，使页面不会乱跑*/
    .addCommodity-box{
        width: 1400px;
    }
    /*添加页面百分百*/
    .addCommodity-one{
        width: 90%;
        height: 50px;
        line-height: 50px;
        background: #F3F3F3;
    }
    /*课程添加*/
    .addCommodity-left{
        float: left;
        padding-left: 20px;
        color: #99A4BF;
    }
    /*返回按钮右浮动*/
    .addCommodity-right{
        float: right;
        padding-right: 130px;
    }
    /*添加课程需要居中四边需要一个边框*/
    .addCommodity-two{
        width: 1000px;
        margin: 0 auto;
        border: 1px solid #E4E4E4;
    }
    /*添加课程信息的样式*/
    .addCommodity-two-xinxi{
        position: relative;
        top: 40px;
        left: -200px;
        float: left;
        width: 230px;
        height: 50px;
        background: rgba(158,167,180,.8);
    }
    /*需要设置一个右边的三角形边框以及需要定位定上去*/
    .addCommodity-two-xinxi .right{
        width: 0;
        height: 0;
        border-top: 30px solid transparent;
        border-left: 26px solid rgba(158,167,180,.8);
        border-bottom: 20px solid transparent;
        font-size: 0;
        line-height: 0;
        position: relative;
        left: 230px;
    }
    /*添加课程信息的文字需要一个定位*/
    .addCommodity-two-ding{
        position: relative;
        top: -35px;
        padding-left: 25px;
    }
    /*课程信息需要右浮动*/
    .addCommodity-two-ding div{
        font-size: 18px;
        color: #fff;
        float: right;
    }
    /*课程的表单信息样式*/
    .addCommodity-two-form{
        padding-top: 100px;
        width: 600px;
    }

    .avatar-uploader-icon {
        border: 1px dashed #d9d9d9 !important;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9 !important;
        border-radius: 6px !important;
        position: relative !important;
        overflow: hidden !important;
    }
    .avatar-uploader .el-upload:hover {
        border: 1px dashed #d9d9d9 !important;
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 300px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 300px;
        height: 178px;
        display: block;
    }

</style>